package io.renren.modules.oa.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.oa.entity.OaPlanConfigEntity;
import io.renren.modules.oa.entity.OaPlanEmployeeEntity;
import io.renren.modules.oa.entity.OaPlanLogEntity;
import io.renren.modules.oa.redis.DingtalkRedis;
import io.renren.modules.oa.service.OaPlanConfigService;
import io.renren.modules.oa.service.OaPlanLogService;
import io.renren.modules.sys.shiro.ShiroUtils;



/**
 * 值班配置
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2018-10-25 11:56:09
 */
@RestController
@RequestMapping("oa/oaplanconfig")
public class OaPlanConfigController {
    @Autowired
    private OaPlanConfigService oaPlanConfigService;
    
    @Autowired
    private OaPlanLogService oaPlanLogService;
    

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("oa:oaplanconfig:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = oaPlanConfigService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @RequiresPermissions("oa:oaplanconfig:info")
    public R info(){
        
        Long merchantId = ShiroUtils.getUserId();	//商户编号
        OaPlanConfigEntity oaPlanConfig = oaPlanConfigService.selectByMerchantId(merchantId);
        //初始化全局配置信息
    	if(oaPlanConfig == null){ 
    		oaPlanConfig = new OaPlanConfigEntity();
    		oaPlanConfig.setMerchantId(merchantId);
    		oaPlanConfig.setCreateTime(new Date());
    		oaPlanConfigService.insert(oaPlanConfig);
        }
        return R.ok().put("oaPlanConfig", oaPlanConfig);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("oa:oaplanconfig:save")
    public R save(@RequestBody OaPlanConfigEntity oaPlanConfig){
    	Long merchantId = ShiroUtils.getUserId();	//商户编号
    	oaPlanConfig.setMerchantId(merchantId);
        oaPlanConfigService.insert(oaPlanConfig);
        DingtalkRedis.delPlanConfig(merchantId);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("oa:oaplanconfig:update")
    public R update(@RequestBody OaPlanConfigEntity oaPlanConfig){
        ValidatorUtils.validateEntity(oaPlanConfig);
       
        
       /* List<OaPlanEmployeeEntity> activeList = DingtalkRedis.queryActiveList(oaPlanConfig.getMerchantId());  
        if(oaPlanConfig.getPlanDate() != null && DateUtils.getStartTime().getTime() <= oaPlanConfig.getPlanDate().getTime()){
        	oaPlanLogService.delList("3", oaPlanConfig.getPlanDate());
        	List<OaPlanLogEntity> qingjiaList = oaPlanLogService.selectList("2", DateUtils.getStartTime(oaPlanConfig.getPlanDate()), DateUtils.getEndTime(oaPlanConfig.getPlanDate()));
    		JSONObject qingjia = new JSONObject();
    		for (OaPlanLogEntity oaPlanLogEntity : qingjiaList) {
    			String data = DateUtils.format(oaPlanLogEntity.getCreateTime());
    			qingjia.put(oaPlanLogEntity.getUserid()+data, data);
    		}
    		Integer no = oaPlanConfig.getNo();
    		if(no == null){
    			oaPlanConfig.setNo(0);
    		}
    		
    		for (int i = 0; i < oaPlanConfig.getDays(); i++) {
    			Date date = DateUtils.addDateMinutes(DateUtils.addDateDays(oaPlanConfig.getPlanDate(), i), 480);
    			for (int j = 0; j < oaPlanConfig.getCount(); j++) {
    				OaPlanEmployeeEntity planEmployee = activeList.get(j);
    				OaPlanLogEntity planLog = oaPlanLogService.selectByUserid(planEmployee.getUserid(),"3", DateUtils.getStartTime(date), DateUtils.getEndTime(date));
					if(planLog == null){
						 planLog = new OaPlanLogEntity();
					}
					planLog.setMerchantId(oaPlanConfig.getMerchantId());
					planLog.setTitle(oaPlanConfig.getTitle());
					planLog.setName(planEmployee.getName());
					planLog.setAvatar(planEmployee.getAvatar());
					planLog.setType("3");	
					planLog.setUserid(planEmployee.getUserid());
					planLog.setCreateTime(date);
					planLog.setDays(1);
			
					oaPlanLogService.insertOrUpdate(planLog);
    			}
    		}
        }*/
        
		
		oaPlanConfigService.updateAllColumnById(oaPlanConfig);//全部更新
		DingtalkRedis.delPlanConfig(oaPlanConfig.getMerchantId());
		DingtalkRedis.delPlanConfigList();
		DingtalkRedis.delActiveList(oaPlanConfig.getMerchantId());
		
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("oa:oaplanconfig:delete")
    public R delete(@RequestBody Long[] ids){
        oaPlanConfigService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    


}
